package puyu.manage.service.common.config;

import cn.hutool.core.collection.CollectionUtil;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import puyu.manage.service.CommonPrint.entity.CommonPrint;
import puyu.manage.service.common.page.PageDomain;
import puyu.manage.service.common.page.TableDataInfo;
import puyu.manage.service.common.page.TableSupport;
import puyu.manage.service.constant.AjaxResult;
import puyu.manage.service.constant.HttpStatus;
import puyu.manage.service.menu.service.SysMenuServiceI;
import puyu.manage.service.print.service.SysPrintnumberService;
import puyu.manage.service.security.SecurityUtils;
import puyu.manage.service.utils.DateUtils;
import puyu.manage.service.utils.SqlUtil;
import puyu.manage.service.utils.StringUtils;
import puyu.manage.service.utils.except.CustomException;

import java.beans.PropertyEditorSupport;
import java.util.*;

/**
 *  @ClassName: Controller层 基础类
 *  @author: whl
 *  @Date: 2024/6/3 9:53
 *  @Description: Description of this class
 */
@Component
public class BaseController {

    @Autowired(required = false)
    private SysMenuServiceI sysMenuServiceI;

    @Autowired(required = false)
    private FineReportConfig fineReportConfig;

    @Autowired
    private SysPrintnumberService sysPrintnumberService;

    /**
     * 外链标识(0表示是外链)
     */
    private static final String FRAME_FLAG ="0";

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtils.parseDate(text));
            }
        });
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }


    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        if (CollectionUtil.isEmpty(list)) {
            rspData.setTotal(0);
        } else {
            rspData.setTotal(new PageInfo(list).getTotal());
        }
        return rspData;
    }
     
    
    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows) {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjaxResult(int rows, Object obj) {
        return rows > 0 ? AjaxResult.success(obj) : AjaxResult.error();
    }


    /**
     * 获取帆软打印路径
     * @param commonPrint
     * @return
     */
    protected String basePrint(CommonPrint commonPrint)throws Exception{
        if(StringUtils.isEmpty(commonPrint.getId())) {
            throw new CustomException ("00100");
        }
        Map map=new HashMap();
        sysPrintnumberService.insertSysPrintnumber(map);

        //获取权限标识
        String perms = commonPrint.getPerms();
        Map<String,Object> sysMenu  = sysMenuServiceI.selectMenuByPerms(perms);
        String redirecturl ="";

        if(null != sysMenu) {
            String isFrame = sysMenu.get("isframe")+"";
            if(FRAME_FLAG.equals(isFrame)) {
                StringBuffer   buffer = new StringBuffer();
                buffer.append(fineReportConfig.getFileStoreDir());
                String path = sysMenu.get("path")+"";
                //判断根路径不为空
                if(org.apache.commons.lang3.StringUtils.isNotBlank(path)) {
                    buffer.append(path);

                } else {
                    buffer.append(fineReportConfig.getDefaultFile());
                }
                buffer.append("&id=");
                buffer.append(commonPrint.getId());
                buffer.append("&userCode=");
                buffer.append(SecurityUtils.getUsername());
                buffer.append("&unitCode=");
                buffer.append(commonPrint.getUnitCode());
                buffer.append("&deptCode=");
                buffer.append(commonPrint.getDeptCode());
                redirecturl =buffer.toString();
            }
        }
        return redirecturl;
    }



    /**
     * 获取帆软粘贴单路径
     * @param commonPrint
     * @return
     */
    protected String basePaste(CommonPrint commonPrint){
        if(StringUtils.isEmpty(commonPrint.getId())) {
            throw new CustomException("00100");
        }
        //获取权限标识
        String perms = commonPrint.getPerms();

        Map<String,Object> sysMenu  = sysMenuServiceI.selectMenuByPerms(perms);
        String redirecturl ="";

        if(null != sysMenu) {
            String isFrame = sysMenu.get("isframe")+"";
            if(FRAME_FLAG.equals(isFrame)) {
                StringBuffer   buffer = new StringBuffer();
                buffer.append(fineReportConfig.getFileStoreDir());
                String path = sysMenu.get("path")+"";
                //判断根路径不为空
                if(org.apache.commons.lang3.StringUtils.isNotBlank(path)) {
                    buffer.append(path);

                } else {
                    buffer.append(fineReportConfig.getDefaultFile());
                }
                buffer.append("&id=");
                buffer.append(commonPrint.getId());
                buffer.append("&userCode=");
                buffer.append(SecurityUtils.getUsername());
                buffer.append("&unitCode=");
                buffer.append(commonPrint.getUnitCode());
                buffer.append("&deptCode=");
                buffer.append(commonPrint.getDeptCode());
                redirecturl =buffer.toString();
            }
        }
        return redirecturl;
    }

      
}
